﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    // Layout = null;
    // var url = Context.Request.GetEncodedPathAndQuery();
    // string d = "";
    //var url = Request.RawUrl;
}
@section css {
    <style>
        .less-query .more {
            display: none;
        }
    </style>
}

<div class="layuimini-container">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <fieldset class="table-search-fieldset" id="tbl-grp-search-fieldset">
                    <legend>搜索信息</legend>
                    <div style="margin: 5px;">
                        <form class="layui-form layui-form-pane less-query" id="frmSearch" action="">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label" style="width:60px">关键字</label>
                                <div class="layui-input-inline" style="width:120px">
                                        <input type="text" name="keyword" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm" lay-submit lay-filter="data-search-btn"><i class="layui-icon layui-icon-search layui-btn-sm"></i> 搜 索</button>
                                    @* <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="btnMoreQuery">
                                <i class="layui-icon layui-icon-down"></i>
                            </button> *@
                                </div>
                            </div>
                        </form>
                    </div>
                </fieldset>

                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                      <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-add-1"></i>添加 </button>
                    </div>
                </script>

                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
                </script> 
         </div>
        <div class="layui-col-md9">
            <fieldset class="table-search-fieldset" id="tbl-dic-search-fieldset">
                <legend id="titleDicGrp">搜索信息</legend>
                <div style="margin: 5px;">
                    <form class="layui-form layui-form-pane less-query" id="frmDicSearch" action="">
                        <input type="hidden" name="DicGrpId" id="DicGrpId" value="" />
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">关键字</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="keyword" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary layui-btn-sm" id="dic-data-search-btn" lay-submit lay-filter="dic-data-search-btn"><i class="layui-icon layui-icon-search layui-btn-sm"></i> 搜 索</button>
                                
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarData">
                <div class="layui-btn-container">
                  <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-add-1"></i>添加 </button>
                </div>
            </script>

            <table class="layui-hide" id="currentDataTableId" lay-filter="currentDataTableFilter"></table>

            <script type="text/html" id="currentDataTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
         </<div>
     </div>
    
</div>
@section js {


    <script>
        var baseUrl = '@(Url.Content("~/"))';
        layui.use(['form', 'table','miniAdmin'], function () {
            var $t = layui.jquery,
                form = layui.form,
                        table = layui.table;
                    var admin = layui.miniAdmin;
            var grpTblId = 'grpTblId';
            var dataTblId = 'dataTblId';
            //
                    function reloadTableGrp(){
                            table.reload(grpTblId);
                    }
                    function reloadTableMain(){
                            table.reload(dataTblId);
                    }
            // $("#btnMoreQuery").on("click", function () {
            //     var t = $(this)
            //     var frmSearch = $("#frmSearch");
            //     var icon = t.find('i');
            //     if (icon.hasClass('layui-icon-up')) {
            //         icon.removeClass('layui-icon-up').addClass('layui-icon-down');
            //         frmSearch.addClass("less-query").removeClass('more-query');
            //     } else {
            //         icon.addClass('layui-icon-up').removeClass('layui-icon-down');
            //         frmSearch.addClass("more-query").removeClass('less-query');
            //     }
            //     $(".table-search-fieldset").trigger( "resize" );
            //     //mainTable.resize();
            // });

             var mainTable = table.render({
                id:grpTblId,
                elem: '#currentTableId',
                url: '@(Url.Content("~/sys/dic/grppage"))',
                method:'post',
                parseData: function (res) { 
                    return {
                        "code": res.resultCode, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.count, //解析数据长度
                        "data": res.data.items //解析数据列表
                    };
                },
                toolbar: '#toolbarDemo',
                defaultToolbar:[],
                height: function () {
                    var height = $("#tbl-grp-search-fieldset").height();
                    // 69 为各个 margin 的和
                    var resultHeight = $(window).height() - height - 35;
                    if (resultHeight < 300) {

                        return 'auto';
                    }
                    return resultHeight;
                }, // 最大高度减去其他容器已占有的高度差
                cols: [[ 
                   // { field: 'id', width: 80, title: 'ID', sort: true },
                    { field: 'title', width: 65, title: '标题' },
                    { field: 'grpKey', width: 70, title: '分组key', sort: true },

                    { field: 'remark', title: '描述', width: 150 },
                    { fixed: 'right', title: '操作', width: 130, toolbar: '#currentTableBar', align: "left" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                autoSort:false, // 服务端排序
                response: {
                    statusCode: 200 // 重新规定成功的状态码为 200，table 组件默认为 0
                }
            });
            // 行单击事件( 双击事件为: rowDouble )
            table.on('row(currentTableFilter)', function(obj){
                var data = obj.data; // 获取当前行数据 
                // console.log(data);
                $("#DicGrpId").val(data.id);
                $("#titleDicGrp").html('搜索分组['+data.title+']信息');
                // 显示 - 仅用于演示
                // layer.msg('当前行数据：<br>'+ JSON.stringify(data), {
                //     offset: '65px'
                // });
                // 标注当前点击行的选中状态
                obj.setRowChecked({
                     type: 'radio' // radio 单选模式；checkbox 复选模式
                });
                $("#dic-data-search-btn").click();
            });

            $(window).on('resize',function(){
                mainTable.resize();
                dicTable.resize();
            });
            $(".table-search-fieldset").on('resize',function(){
                // console.log('.table-search-fieldset resize:', $(".table-search-fieldset").height())
                mainTable.resize();
                dicTable.resize();
            });

            // 触发排序事件
            table.on('sort(currentTableFilter)', function (obj) {

                var where = obj.config.where;
                where.SortField = obj.field

                if (where.SortField) {
                    where.SortType = obj.type
                }
                var option = table.getOptions(grpTblId);
                //执行搜索重载
                table.reload(grpTblId, {
                    initSort: obj, // 记录初始排序，如果不设的话，将无法标记表头的排序状态。
                    where: where
                }, 'data');
            });
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //var result = JSON.stringify(data.field);
                // layer.alert(result, {
                //     title: '最终的搜索信息'
                // });
                // 获取配置项
                var $option = table.getOptions(grpTblId);
                // console.log('获取配置项', $option);
                // 复制搜索条件
                var where = JSON.parse(JSON.stringify(data.field));
                if ($option.where && $option.where.SortField) {
                    where.SortField = $option.where.SortField
                    where.SortType = $option.where.SortType
                }
                //执行搜索重载
                table.reload(grpTblId, {
                    page: {
                        curr: 1
                    },
                    where: where
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加分组',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/dic/grpedit/"))',
                        end: function(e){
                          //console.log('关闭编辑');
                          // 紧紧重新加载数据
                                          reloadTableGrp();
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                }
            });
  
            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                // console.log('数据', data, obj);
                if (obj.event === 'edit') { 
                    var index = layer.open({
                        title: '编辑分组',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/dic/grpedit/"))' + data.id,
                        end: function(e){
                          //console.log('关闭编辑');
                          // 紧紧重新加载数据
                                  table.reloadData(grpTblId);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                               // console.log('数据', data, obj);
                       obj.del();
                       layer.close(index);
                       var loadIndex = layer.load(3);
                       $.ajax({
                            url: '@(Url.Content("~/sys/dic/GrpDelete/"))'+data.id,
                            // data: data,
                            method: 'post',
                            success: function (d) {
                                // console.log(d);
                                // l.destroy();
                                layer.close(loadIndex);
                                if (d.resultCode == "200") { 
                                    layer.msg('删除成功');
                                    reloadTableGrp();
                                    reloadTableMain();
                                } else {

                                    layer.msg(d.message); 
                                }
                                // alert(d)
                            },
                            error: function (e) {
                                // console.log(e);
                                layer.close(loadIndex);
                            }
                       }); 
                       
                    });
                }
            });

                     // 数据表格
                     var dicTable = table.render({
                        id:dataTblId,
                        elem: '#currentDataTableId',
                        url: '@(Url.Content("~/sys/dic/page"))',
                        method:'post',
                        parseData: function (res) {
                            return {
                                "code": res.resultCode, //解析接口状态
                                "msg": res.message, //解析提示文本
                                "count": res.data.count, //解析数据长度
                                "data": res.data.items //解析数据列表
                            };
                        },
                                        toolbar: '#toolbarData',
                        defaultToolbar:[],
                        height: function () {
                            var height = $("#tbl-dic-search-fieldset").height();
                            // 69 为各个 margin 的和
                            var resultHeight = $(window).height() - height - 35;
                            if (resultHeight < 300) {

                                return 'auto';
                            }
                            return resultHeight;
                        }, // 最大高度减去其他容器已占有的高度差
                        cols: [[
                           // { field: 'id', width: 80, title: 'ID', sort: true },
                            { field: 'dicTitle', width: 70, title: '标题', sort: true },
                            { field: 'dicValue', width: 65, title: '值' },
                            { field: 'remark', title: '描述', width: 150 },
                            { fixed: 'right', title: '操作', width: 130, toolbar: '#currentDataTableBar', align: "left" }
                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        autoSort:false, // 服务端排序
                        response: {
                            statusCode: 200 // 重新规定成功的状态码为 200，table 组件默认为 0
                        }
                    });


                            // 行单击事件( 双击事件为: rowDouble )
                    table.on('row(currentDataTableFilter)', function(obj){
                        var data = obj.data; // 获取当前行数据
                        // 显示 - 仅用于演示
                        // layer.msg('当前行数据：<br>'+ JSON.stringify(data), {
                        //     offset: '65px'
                        // });
                        // 标注当前点击行的选中状态
                        obj.setRowChecked({
                             type: 'radio' // radio 单选模式；checkbox 复选模式
                        });
                    });


            // 触发排序事件
            table.on('sort(currentDataTableFilter)', function (obj) {

                var where = obj.config.where;
                where.SortField = obj.field

                if (where.SortField) {
                    where.SortType = obj.type
                }
                var option = table.getOptions(dataTblId);
                //执行搜索重载
                table.reload(dataTblId, {
                    initSort: obj, // 记录初始排序，如果不设的话，将无法标记表头的排序状态。
                    where: where
                }, 'data');
            });
                     
            // 监听搜索操作
            form.on('submit(dic-data-search-btn)', function (data) {
                //var result = JSON.stringify(data.field);
                // layer.alert(result, {
                //     title: '最终的搜索信息'
                // });
                // 获取配置项
                        var $option = table.getOptions(dataTblId);
                // console.log('获取配置项', $option);
                // 复制搜索条件
                var where = JSON.parse(JSON.stringify(data.field));
                if ($option.where && $option.where.SortField) {
                    where.SortField = $option.where.SortField
                    where.SortType = $option.where.SortType
                }
                //执行搜索重载
                table.reload(dataTblId, {
                    page: {
                        curr: 1
                    },
                    where: where
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentDataTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var grpid = $("#DicGrpId").val();
                    if(grpid =='' || grpid == '0'){
                                  admin.error('请先选择分组');
                                  return ;
                    }
                    var index = layer.open({
                        title: '添加字典',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/dic/edit?id=0&grpid="))'+grpid,
                        end: function(e){
                          //console.log('关闭编辑');
                          // 紧紧重新加载数据
                          table.reloadData(dataTblId);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('currentDataTableId') , data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                }
            });

            table.on('tool(currentDataTableFilter)', function (obj) {
                var data = obj.data;
                // console.log('数据', data, obj);
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/dic/edit/"))' + data.id,
                        end: function(e){
                          //console.log('关闭编辑');
                          // 紧紧重新加载数据
                          table.reloadData(dataTblId);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                     layer.confirm('真的删除行么', function (index) {
                               // console.log('数据', data, obj);
                       obj.del();
                       layer.close(index);
                       var loadIndex = layer.load(3);
                       $.ajax({
                            url: '@(Url.Content("~/sys/dic/Delete/"))'+data.id,
                            // data: data,
                            method: 'post',
                            success: function (d) {
                                // console.log(d);
                                // l.destroy();
                                layer.close(loadIndex);
                                if (d.resultCode == "200") { 
                                    layer.msg('删除成功');
                                    table.reload(dataTblId);
                                } else {

                                    layer.msg(d.message); 
                                }
                                // alert(d)
                            },
                            error: function (e) {
                                // console.log(e);
                                layer.close(loadIndex);
                            }
                       }); 
                       
                    }); 
                }
            });


        });
    </script>
}